import {Component, OnInit} from '@angular/core';
import {Location} from '@angular/common';
import {ActivatedRoute} from '@angular/router';
import {HttpClient} from '@angular/common/http';
import {NzMessageService} from 'ng-zorro-antd';

@Component({
  selector: 'app-detail',
  templateUrl: './detail.component.html',
  styleUrls: ['./detail.component.scss']
})
export class DetailComponent implements OnInit {
  refreshLoadingFlag = false;
  editLoadingFlag = false;
  id;
  info = {};
  installLoading = false;

  constructor(
    private location: Location,
    private route: ActivatedRoute,
    private http: HttpClient,
    private message: NzMessageService,
  ) {
  }

  ngOnInit() {
    this.route.paramMap.subscribe(params => {
      this.id = params.get('id');
      this.http.get('/api/initialization/getServer/' + this.id).subscribe(res => {
        this.info = res;
      });
    });
  }

  backward() {
    this.location.back();
  }

  refresh() {
    this.refreshLoadingFlag = true;
    this.http.post('/api/initialization/refreshServer/' + this.id, null).subscribe(res => {
      this.message.create('success', `服务器刷新成功`);
      this.info = res;
      this.refreshLoadingFlag = false;
    }, () => {
      this.message.create('error', `服务器连接失败`);
      this.refreshLoadingFlag = false;
    });
  }

  edit() {
    this.editLoadingFlag = true;
    setTimeout(() => {
      this.editLoadingFlag = false;
    }, 1000);
  }

}
